import Vue from 'vue'
import VueRouter from 'vue-router'
import parentView from 'components/parent-view'
import config from 'api/config.js'

import GroundInfo from '../views/GroundInfo/GroundInfo.vue'
import DingChang from '../views/DingChang/DingChang.vue'
import AddPlayer from '../views/AddPlayer/AddPlayer.vue'
import User from '../views/User/User.vue'
import OrderList from '../views/OrderList/OrderList.vue'
import OrderDetail from '../views/OrderDetail/OrderDetail.vue'

import store from 'store'

Vue.use(VueRouter)

const routes = [
  {
    path: config.routerUrl,
    name: 'm',
    component: parentView,
    children:[
      {
        path:'login',
        name:'login',
        meta:{
          title:'登入',
          keepAlive:true
        },
        component:()=>import('../views/Login/Login.vue')
      },
      {
        path:'register',
        name:'register',
        meta:{
          title:'注册',
          keepAlive:true
        },
        component:()=>import('../views/register/register.vue')
      },
      {
        path:'ball/:courseid',
        name:'ball',
        meta:{
          title:'首页',
          keepAlive:true
        },
        component:()=>import('../views/ball/index.vue')
      },
      {
        path:'card/:id',
        name:'card',
        meta:{
          title:'会籍卡',
          keepAlive:true
        },
        component:()=>import('../views/ball/card.vue')
      },
      {
        path:'imgList/:id/:type',
        name:'imgList',
        meta:{
          title:'图集',
          keepAlive:true
        },
        component:()=>import('../views/ball/imgList.vue')
      },
      {
        path:'qiudong/:id',
        name:'qiudong',
        meta:{
          title:'球洞',
          keepAlive:true
        },
        component:()=>import('../views/ball/qiudong.vue')
      },
      {
        path:'user',
        name:'user',
        meta:{
          title:'个人中心',
        },
        component: User
      },
      {
        path:'userEdit',
        name:'userEdit',
        meta:{
          title:'修改资料',
        },
        component:()=>import('../views/ball/userEdit.vue')
      },
      {
        path:'couponList',
        name:'couponList',
        meta:{
          title:'优惠券列表',
        },
        component:()=>import('../views/ball/couponList.vue')
      },
      {
        path:'meal/:id',
        name:'meal',
        meta:{
          title:'套餐',
          keepAlive:true
        },
        component:()=>import('../views/ball/meal.vue')
      },
      {
        path:'mealList',
        name:'mealList',
        meta:{
          title:'套餐列表',
          keepAlive:true
        },
        component:()=>import('../views/ball/mealList.vue')
      },
      {
        path:'destineMeal',
        name:'destineMeal',
        meta:{
          title:'预订套餐',
          keepAlive:true
        },
        component:()=>import('../views/ball/destineMeal.vue')
      },
      {
        path:'chooseDate',
        name:'chooseDate',
        meta:{
          title:'选择日期',
          keepAlive:true
        },
        component:()=>import('../views/ball/chooseDate.vue')
      },
      {
        path:'yudingDate',
        name:'yudingDate',
        meta:{
          title:'预订日期',
          keepAlive:true
        },
        component:()=>import('../views/ball/yudingDate.vue')
      },
      {
        path: 'groundinfo/:id',
        name: 'groundInfo',
        meta: {
          title: "球场信息",
          keepAlive: true,
        },
        component: GroundInfo
      },
      {
        path: 'dingchang',
        name: 'dingchang',
        meta: {
          title: "球场预订",
          keepAlive: true,
        },
        component: DingChang
      },
      {
        path: 'addplayer',
        name: 'AddPlayer',
        meta: {
          title: "添加打球人",
          keepAlive: true,
        },
        component: AddPlayer
      },
      {
        path: 'orderlist/:type',
        name: 'OrderList',
        meta: {
          title: "订单列表",
          keepAlive: true,
        },
        component: OrderList
      },
      {
        path: 'orderdetail/:id',
        name: 'OrderDetail',
        meta: {
          title: "订单详情",
          keepAlive: true,
        },
        component: OrderDetail
      },
      {
        path: 'confirmOrder/:id',
        name: 'confirmOrder',
        meta: {
          title: "确认新订单",
          keepAlive: true,
        },
        component: ()=>import("../views/confirm/confirmOrder.vue")
      },
      {
        path: 'confirmTuikuan/:id',
        name: 'confirmTuikuan',
        meta: {
          title: "退款成功",
          keepAlive: true,
        },
        component: ()=>import("../views/confirm/confirmTuikuan.vue")
      },
      {
        path:'/',
        name:'store_list',
        meta:{
          title:'球场列表',
          keepAlive:true
        },
        component:()=>import('../views/store_list/store_list.vue')
      },
      {
        path:'go2buy/:order_id',
        name:'go2buy',
        meta:{
          title:'在线支付',
          keepAlive:true
        },
        component:()=>import('../views/ball/go2buy.vue')
      },
      {
        path:'*',
        name:'404',
        component:()=>import('../views/404.vue')
      },
    ]
  },
]

//以下代码解决路由地址重复的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  let addPaddingName = ["store_list","user",'dingchang','destineMeal']
  let disableBottom = ["chooseDate","yudingDate"]
  let toName = ["store_list","ball","login","register","confirmOrder","confirmTuikuan","imgList","groundInfo","qiudong"]
  // let toName = ["dingchang","destineMeal","meal","go2buy"]
  let fromName = ["login","register"]
  // console.log(to,from)
  if(!toName.find(e=>e == to.name) && from.name == null){
    store.dispatch("common/init")
  }
  if(fromName.find(e=>e == from.name) && !fromName.find(e=>e == to.name)){
    // store.dispatch("common/init")
  }
  if(addPaddingName.find(e=>e == to.name)){
    store.commit("common/setAddPadding",true)
  }else{
    store.commit("common/setAddPadding",false)
  }
  if(!disableBottom.find(e=>e == to.name)){
    store.commit("common/setShowBottom",true)
  }else{
    store.commit("common/setShowBottom",false)
  }
  if(to.meta.title){
    document.title = to.meta.title
  }
  document.documentElement.scrollTop = 0
  next()
})

router.afterEach((to,form)=>{
  let checkLoginName = ['AddPlayer','go2buy','orderlist']
  let footerShowList = ['store_list','user']
  // let checkLoginName = []
  if(footerShowList.find(e=>e == to.name)){
    store.state.common.footerShow = true
  }else{
    store.state.common.footerShow = false
  }
  if(checkLoginName.find(e=>e == to.name)){
    // store.dispatch('user/checkLogin')
  }
})

export default router
